package app.foodnote;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import app.foodnote.db.FoodNote;
import app.foodnote.db.FoodNotesDataSource;

public class FoodAdd extends Activity implements LocationListener {
	private static final String TAG = "SaveFoods";
	private FoodNotesDataSource datasource;

	private EditText name;
	private EditText address;
	private EditText phonenumber;
	private AutoCompleteTextView cities;
	private EditText othernotes;
	private TextView log;
	private TextView lat;
	
	LocationManager locationManager;
	Bundle bundle;
	
	private String[] citiesArray = {"An Giang", "Bà Rịa - Vũng Tàu", "Bạc Liêu", "Bắc Giang" , "Bắc Kạn",
			"Bắc Ninh", "Bến Tre", "Bình Dương ", "Bình Định" , "Bình Phước", " Bình Thuận", 
			"Cà Mau", "Cao Bằng", "Cần Thơ", "Đà Nẵng", "Đắk Lắk", "Đắk Nông", "Điện Biên", "Đồng Nai",
			"Đồng Tháp", "Gia Lai", "Hà Giang", "Hà Nam", "Hà Nội", "Hà Tĩnh", "Hải Dương", "Hải Phòng",
			"Hậu Giang", "Hòa Bình", "Hưng Yên", "Khánh Hòa", "Kiên Giang", "Kon Tum", "Lai Châu", "Lạng Sơn",
			"Lào Cai", "Lâm Đồng", "Long An", "Nam Định", "Nghệ An", "Ninh Bình", "Ninh Thuận", "Phú Thọ",
			"Phú Yên", "Quảng Bình", "Quảng Nam", "Quảng Ngãi", "Quảng Ninh", "Quảng Trị", "Sóc Trăng",
			"Sơn La", "Tây Ninh", "Thái Bình", "Thái Nguyên", "Thanh Hóa", "Thành phố Hồ Chí Minh",
			"Thừa Thiên - Huế", "Tiền Giang", "Trà Vinh", "Tuyên Quang", "Vĩnh Long", "Vĩnh Phúc", "Yên Bái"
			};
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Thêm món ăn");
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.form_add);
		
		name = (EditText) findViewById(R.id.EditName);
		address = (EditText) findViewById(R.id.EditAddr1);
		phonenumber = (EditText) findViewById(R.id.EditTel);
		cities = (AutoCompleteTextView) findViewById(R.id.autocomplete_cities);
		othernotes = (EditText) findViewById(R.id.EditNotes);
		log = (TextView) findViewById(R.id.log);
		lat = (TextView) findViewById(R.id.lat);
		
		datasource = new FoodNotesDataSource(this);

		ArrayAdapter<String> cities_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, citiesArray);
		cities.setThreshold(1);
		cities.setAdapter(cities_adapter);

		// Find Current Location
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location location = locationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (location != null) {
			Log.d("Location", location.toString());
			this.onLocationChanged(location);
		}
		
	
			
	}

	private ArrayList<String> getIntentValues(){
		ArrayList<String> values = new ArrayList<String>();
		bundle = getIntent().getExtras();
		String txtId = bundle.getString("Id");
		String txtName = bundle.getString("name");
		String txtAddress = bundle.getString("address");
		String txtCity = bundle.getString("city");
		String txtPhoneNumber = bundle.getString("phonenumber");
		String txtOtherNote = bundle.getString("othernote");
		
		values.add(txtId);
		values.add(txtName);
		values.add(txtAddress);
		values.add(txtCity);
		values.add(txtPhoneNumber);
		values.add(txtOtherNote);
		
		return values;
	}
	private void setEditValues(){
		ArrayList<String>  getValues = getIntentValues();
		name.setText(getValues.get(1));
		address.setText(getValues.get(2));
		cities.setText(getValues.get(3));
		phonenumber.setText(getValues.get(4));
		othernotes.setText(getValues.get(5));
	}
	
	@Override
	protected void onResume() {
		
		super.onResume();
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); 
	}

	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this); 
	}
	@Override
	protected void onDestroy(){
		super.onDestroy();
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.form_add_option, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.imap) {
			startActivity(new Intent(FoodAdd.this, LocationActivity.class));
		} else if (item.getItemId() == R.id.Save) {
			SaveFood();
		}
		return super.onOptionsItemSelected(item);
	}

	// put a foods data into database
	private void SaveFood() {
		
		String message = "Đã lưu thông tin món ăn.";
		
		FoodNote foodnote = new FoodNote(name.getText().toString(), address.getText().toString(), 
				phonenumber.getText().toString(), cities.getText().toString(),
				othernotes.getText().toString(), 0, 0);
		datasource.openToWrite();
		datasource.addFoodNotes(foodnote);
		datasource.close();
		
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
		Log.w(TAG, "Luu thanh cong!");
	}

	public void onLocationChanged(Location location) {
		if (location != null) {
			Log.d("LOCATION CHANGED", location.getLatitude() + "");
			Log.d("LOCATION CHANGED", location.getLongitude() + "");
			Toast.makeText(FoodAdd.this,
					location.getLatitude() + "" + location.getLongitude(),
					Toast.LENGTH_LONG).show();
			String log = String.valueOf(location.getLongitude());
			String lat = String.valueOf(location.getLatitude());
			this.log.setText(log);
			this.lat.setText(lat);
		}

	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
	// update foods
	public void updateFoodInfo(){
		datasource.openToWrite();
		//datasource.updateFoodNote(foodNote);
		datasource.close();
	}

}
